package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.Department;
import com.sqx.modules.health.entity.Hospital;
import com.sqx.modules.health.service.IDepartmentService;
import com.sqx.modules.health.service.IHospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院科室表 前端控制器
 * </p>
 *
 * @author gxy
 * @since 2022-06-13
 */

@Api(value = "医院科室表", tags = "医院科室接口", description = "")
@RestController
@RequestMapping("/health/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IHospitalService hospitalService;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新医院科室表", notes = "新增或更新医院科室表")
    public Result save(@RequestBody Department department) {
        return Result.success(departmentService.saveOrUpdate(department));
    }

    // 根据ID删除
    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除医院科室表", notes = "根据ID删除医院科室表")
    public Result delete(@PathVariable Integer id) {
        return Result.success(departmentService.removeById(id));
    }

    // 根据IDS删除
    @GetMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除医院科室表", notes = "根据IDS删除医院科室表")
    public Result deleteBatch(@RequestParam List<Integer> ids) {
        return Result.success(departmentService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部医院科室表", notes = "查询全部医院科室表")
    public Result findAll() {
        return Result.success(departmentService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录医院科室表", notes = "根据ID查询一条记录医院科室表")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(departmentService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询医院科室表", notes = "分页查询医院科室表")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false, defaultValue = "") String departName,
                           @RequestParam(required = false, defaultValue = "") Long hid) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(departName), "name", departName);
        queryWrapper.orderByAsc("id");
        if (ObjectUtil.isNotEmpty(hid)) {
            queryWrapper.eq("hid", hid);
        }
        Page<Department> page = departmentService.page(new Page<>(pageNum, pageSize), queryWrapper);
        //List<DepartmentVo> result = MyBeanUtil.collectionCopy(page.getRecords(), DepartmentVo.class);
        page.getRecords().forEach(item -> {
            Hospital hospital = hospitalService.getById(item.getHid());
            if (ObjectUtil.isNotEmpty(hospital)) {
                item.setHName(hospital.getName());
            }
        });
        return Result.success(page);
    }

    @GetMapping("/findByHid")
    @ApiOperation(value = "根据医院id查询科室", notes = "根据医院id查询科室")
    public Result findPage(@RequestParam(required = false, defaultValue = "") Long hid) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>()
                .eq(ObjectUtil.isNotEmpty(hid), "hid", hid);
        return Result.success(departmentService.list(queryWrapper));
    }

}

